<script setup name="Record">
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/le/record"

const { proxy } = getCurrentInstance()
const { refund_status } = proxy.useDict('refund_status')
const { refund_order_status} = proxy.useDict('refund_order_status')


const recordList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const recordform = ref({})
const total = ref(0)
const title = ref("")
const daterangeApplicationTime = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    refundNo: null,
    orderNo: null,
    applicant: null,
    applicationName:null,
    applicationTime: null,
    refundOrderStatus:null,
    refundStatus: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询退款记录列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}
  if (null != daterangeApplicationTime && '' != daterangeApplicationTime) {
    queryParams.value.params["beginApplicationTime"] = daterangeApplicationTime.value[0]
    queryParams.value.params["endApplicationTime"] = daterangeApplicationTime.value[1]
  }
  listRecord(queryParams.value).then(response => {
    recordList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    refundNo: null,
    orderNo: null,
    refundAmount: null,
    applicant: null,
    applicationTime: null,
    refundTime: null,
    refundOrderStatus: null,
    refundStatus: null,
    refundReason: null,
    refundChannel: null,
    refundMethod: null,
    failureStatusCode: null,
    failureReason: null,
    extraField1: null,
    extraField2: null
  }
  proxy.resetForm("recordRef")
}

function handleStatusChange(value) {
      // 直接调用原有的查询方法，使用已有的queryParams和接口
      queryParams.value.refundStatus = value;
      this.handleQuery();
    }

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeApplicationTime.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加退款记录"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getRecord(_id).then(response => {
    recordform.value = response.data
    open.value = true
    title.value = "修改退款记录"
  })
  
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["recordRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除退款记录编号为"' + _ids + '"的数据项？').then(function() {
    return delRecord(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('le/record/export', {
    ...queryParams.value
  }, `record_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true"
    style="border-bottom: 1px solid #c9c9c9; margin-bottom: 30px"
    v-show="showSearch" label-width="68px">
      <el-form-item label="退款编号" style="margin-right:50px;" prop="refundNo">
        <el-input
          v-model="queryParams.refundNo"
          placeholder="请输入退款编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" style="margin-right:50px;" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" style="margin-right:50px;" prop="applicationName">
        <el-input
          v-model="queryParams.applicationName"
          placeholder="请输入申请人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeApplicationTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="background-color: #266FE8; color: white;" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="status-buttons">
      <el-button
        v-for="dict in refund_status"
        :key="dict.value"
        :class="{
          'active': queryParams.refundStatus === dict.value,
          'default-btn': queryParams.refundStatus !== dict.value
        }"
        @click="handleStatusChange(dict.value)"
      >
        {{ dict.label }}
      </el-button>
      <!-- 全部按钮 -->
      <el-button
        :class="{
          'active': queryParams.orderStatus === '',
          'default-btn': queryParams.orderStatus !== ''
        }"
        @click="handleStatusChange('')"
      >
        全部
      </el-button>
    </div>  

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="退款编号" align="center" prop="refundNo" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column label="申请人" align="center" prop="sysUser.nickName" />
      <el-table-column label="申请时间" align="center" prop="applicationTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applicationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.refundTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款订单状态" align="center" prop="refundOrderStatus" >
        <template #default="scope">
          <dict-tag :options="refund_order_status" :value="scope.row.refundOrderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="退款状态" align="center" prop="refundStatus">
        <template #default="scope">
          <dict-tag :options="refund_status" :value="scope.row.refundStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['le:record:edit']">查看</el-button>
         
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退款记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="recordRef" :model="recordform" :rules="rules" label-width="80px">
        <el-form-item label="退款编号" prop="refundNo">
          {{ recordform.refundNo || '无信息'}}
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          {{ recordform.orderNo || '无信息'}}
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          {{ recordform.refundAmount || '无信息'}}
        </el-form-item>
        <el-form-item label="申请人" prop="applicant">
          {{ recordform.sysUser.nickName || '无信息' }}
        </el-form-item>
        <el-form-item label="申请时间" prop="applicationTime">
          {{ parseTime(recordform.applicationTime, '{y}-{m}-{d}') }}
        </el-form-item>
        <el-form-item label="退款时间" prop="refundTime" >
          {{ parseTime(recordform.refundTime, '{y}-{m}-{d}') }}
        </el-form-item>
        <el-form-item label="退款状态" prop="refundStatus" 
        :style="{ color: recordform.refundStatus == 0 ? '#FFCC00' : 
          recordform.refundStatus == 1 ? '#00CC66' : 
          recordform.refundStatus == 2 ? '#FF3333' : '' }"
          >
           {{ recordform.refundStatus == 0 ? '退款处理中' : 
      recordform.refundStatus == 1 ? '退款成功' : 
      recordform.refundStatus == 2 ? '退款失败' : '无信息' }}
        </el-form-item>
        <el-form-item label="退款原因" prop="refundReason">
          {{ recordform.refundReason || '无信息' }}
        </el-form-item>
        <el-form-item label="退款渠道" prop="refundChannel">
          {{ recordform.refundChannel || '无信息' }}
        </el-form-item>
        <el-form-item label="退款方式" prop="refundMethod">
          {{ recordform.refundMethod || '无信息' }}
        </el-form-item>
        <el-form-item label="失败状态码" prop="failureStatusCode">
          {{ recordform.failureStatusCode || '无信息' }}
        </el-form-item>
        <el-form-item label="失败原因" prop="failureReason">
          {{ recordform.failureReason || '无信息' }}
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.status-buttons {
  display: flex;
  gap: 0; /* 取消按钮组的间距 */
  background-color: #F2F2F2;
  width: 400px;
  flex-wrap: wrap;
  padding: 10px 0;
  border-radius: 10px;
  margin-bottom: 10px;
}
/* 按钮样式 */
:deep(.el-button) {
  padding: 6px 16px;
  border-radius: 4px;
  transition: all 0.2s ease;
}

/* 默认按钮样式 */
.default-btn {
  background-color: #F2F2F2;
  color: #000000;
  border: 1px solid #F2F2F2;
}

.default-btn:hover {
  background-color: #e9ecef;
  color: #409eff;
  border-color: #c6e2ff;
}

/* 选中按钮样式 */
.active {
  background-color: white;
  color: #000000;
  border-color: white;
}

.active:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}
</style>

